#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>
#include<fcntl.h>


#define N 128

char buf[N+1];

char* Strtok(char* str,char* separator)
{
    static char* last_str = NULL;
    if(str == NULL && last_str == NULL)
        return NULL;
    if(str != NULL)//第一次调用函数
    {
        char* tmp = str;
        int len = strlen(separator);
        while((*tmp) != '\0')
        {
            for(int i = 0; i < len; ++i)
            {
                if(*tmp == separator[i])
                {
                    last_str = tmp+1;
                    *tmp = '\0';
                    return str;//代表第一次找到分隔符前面的字符串
                }
            }
            ++tmp;
        }
        //这里代表第一次调用没有遇到分隔符
        return str;
    }
    else
    {
        char* tmp = last_str;
        str = last_str;
        int len = strlen(separator);
        while(*tmp)
        {
            for(int i = 0; i < len; ++i)
            {
                if(*tmp == separator[i])
                {
                    last_str = tmp+1;
                    *tmp = '\0';
                    return str;
                }
            }
            ++tmp;
        }
        last_str = NULL;
        return str;
    }
}

char* command_argv[32];
char copy_buf[N+1];

int main(int argc,char* argv[])
{
    while(1)
    {
        printf("[林深@virtual-machine]# ");
        fflush(stdout);
        fgets(buf,N,stdin);
        buf[strlen(buf)-1] = '\0';

        //添加重定向功能
        char* start = buf;
        int type = -1; //0为输出重定向，1为追加，2为输入
        while(*start != '\0')
        {
            if(*start == '>')
            {
                type = 0;
                *start = '\0';
                ++start;
                if(*start == '>')
                {
                    type = 1;
                    ++start;
                }
                break;
            }
            else if(*start == '<')
            {
                type = 2;
                *start = '\0';
                ++start;
                break;
            }
            ++start;
        }
        if(*start != '\0')
        {
            while(*start == ' ')
                ++start;
        }
        else
            start = NULL;

        strcpy(copy_buf,buf);
        command_argv[0] = strtok(buf," ");
        if(strcmp(command_argv[0],"echo") == 0)
        {
            if(type == -1)
            {
                for(int i = 5; i < strlen(copy_buf); ++i)
                    printf("%c",copy_buf[i]);
                printf("\n");
                continue;
            }
        }
        if(strcmp(command_argv[0],"exit") == 0)
            exit(0);
        int i = 1;
        //ls 加上色彩
        if(strcmp(command_argv[0],"ls") == 0)
            command_argv[i++] = "--color=auto";
        while((command_argv[i++] = Strtok(NULL," ")) != NULL);
        //当前已经分割好字符串
        if(strcmp(command_argv[0],"cd") == 0 && command_argv[1] != NULL)
        {
            chdir(command_argv[1]);
            continue;
        }
        //开始进程替换
        int pid = fork();
        if(pid == 0)//子进程
        {
            if(start != NULL)
            {

                if(type == 0)
                {
                    int fd = open(start,O_CREAT|O_WRONLY|O_TRUNC,0664);
                    close(1);
                    dup2(fd,1);
                }
                else if(type == 1)
                {
                    int fd = open(start,O_CREAT|O_APPEND,0664);
                    close(1);
                    dup2(fd,1);
                }
                else if(type == 2)
                {
                    int fd = open(start,O_RDONLY);
                    close(0);
                    dup2(fd,0);
                }
            }
            if(strcmp(command_argv[0],"echo") == 0 && type >= 0)
            {
                printf("%s",copy_buf+5);
                printf("\n");
                exit(0);
            }
            execvp(command_argv[0],command_argv);
        }
        waitpid(pid,NULL,0);
    }
    return 0;
}